package dyyx;

import java.io.BufferedReader;
import java.io.File;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import dyyx.util.CommUtil;
import dyyx.util.DataUtil;
import dyyx.util.JdbcUtil;

public class DataImport {

	

	private static final String file = System.getProperty("file");
	private static final String table = System.getProperty("table");
	private static final int batchsize = CommUtil.getIntFromSystemProp("batchsize", 1000);
	
	
	private static int lineNum = 0;
	private static int parseErrorNum = 0;
	private static int parseSuccessNum = 0;


	public static void main(String[] args) throws Exception {
		// System.out.println("hello,clickhouse,"+new Date());

		if (CommUtil.isBlank(file)) {
			System.out.println("file blank");
			return;
		}
		

		if (CommUtil.isBlank(table)) {
			System.out.println("table blank");
			return;
		}
		

		if (batchsize < 10) {
			System.out.println("batchsize must >=10");
			return;
		}
		
		System.out.println("file=" + file);
		System.out.println("table=" + table);
		System.out.println("batchsize=" + batchsize);


		
		File f = new File(file);
		if(!f.exists()){
			System.out.println("file not exist");
			return;
		}

		BufferedReader br = null;
		Connection cn = null;

		long start = System.currentTimeMillis();
		
	    String insertSql = DataUtil.getInsertSql(table);
	    

		try {
			br = CommUtil.createBufferedReader(file, null);
			cn = JdbcUtil.getConn();	
			String line = null;
			Data data = null;
			List<Data> list = new ArrayList<Data>();
			
			while((line=br.readLine())!=null){
				lineNum++;
				
				if((lineNum % 1000)==0){
					long end =  System.currentTimeMillis();
					long time = end - start;
					System.out.println("time=" + time+","+getInfo());
				}
				
				data = DataUtil.parseData(line);
				if(data==null){		
					parseErrorNum++;
					continue;
				}
				
				parseSuccessNum++;
				
				list.add(data);
				if(list.size() >= batchsize){
					//  batch insert
					DataUtil.inerst(cn, insertSql, list);
					list = new ArrayList<Data>();
				}
			}
			
			if(!list.isEmpty()){
				// batch insert
				DataUtil.inerst(cn, insertSql, list);
			}

			
		} finally {
			JdbcUtil.close(cn);
			CommUtil.close(br);
		}
		long end = System.currentTimeMillis();
		long time = end - start;	
		System.out.println("data import done,time=" + time+","+getInfo());

	}
	
	private static Map getInfo(){
		Map map = new LinkedHashMap();
		map.put("table", table);
		map.put("batchsize", batchsize);
		map.put("lineNum", lineNum);
		map.put("parseErrorNum", parseErrorNum);
		map.put("parseSuccessNum", parseSuccessNum);
		map.put("file", file);
		
		return map;
	}



}
